package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.TWarningVo;
import com.ruoyi.system.domain.bo.TWarningBo;
import com.ruoyi.system.service.ITWarningService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预警
 *
 * @author ppgod
 * @date 2024-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/warning")
public class TWarningController extends BaseController {

    private final ITWarningService iTWarningService;

    /**
     * 查询预警列表
     */
    @SaCheckPermission("system:warning:list")
    @GetMapping("/list")
    public TableDataInfo<TWarningVo> list(TWarningBo bo, PageQuery pageQuery) {
        return iTWarningService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出预警列表
     */
    @SaCheckPermission("system:warning:export")
    @Log(title = "预警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TWarningBo bo, HttpServletResponse response) {
        List<TWarningVo> list = iTWarningService.queryList(bo);
        ExcelUtil.exportExcel(list, "预警", TWarningVo.class, response);
    }

    /**
     * 获取预警详细信息
     *
     * @param pkid 主键
     */
    @SaCheckPermission("system:warning:query")
    @GetMapping("/{pkid}")
    public R<TWarningVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String pkid) {
        return R.ok(iTWarningService.queryById(pkid));
    }

    /**
     * 新增预警
     */
    @SaCheckPermission("system:warning:add")
    @Log(title = "预警", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TWarningBo bo) {
        return toAjax(iTWarningService.insertByBo(bo));
    }

    /**
     * 修改预警
     */
    @SaCheckPermission("system:warning:edit")
    @Log(title = "预警", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TWarningBo bo) {
        return toAjax(iTWarningService.updateByBo(bo));
    }

    /**
     * 删除预警
     *
     * @param pkids 主键串
     */
    @SaCheckPermission("system:warning:remove")
    @Log(title = "预警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pkids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] pkids) {
        return toAjax(iTWarningService.deleteWithValidByIds(Arrays.asList(pkids), true));
    }
}
